package com.kt.uokos.yimo.wx.controller;


import com.kt.uokos.yimo.util.RegexUtil;
import com.kt.uokos.yimo.util.ResponseUtil;
import com.kt.uokos.yimo.wx.annocation.LoginUser;
import com.kt.uokos.yimo.wx.dao.Address;
import com.kt.uokos.yimo.wx.service.AddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/wx/address")
@Validated
public class AddressController {

    @Resource
    private AddressService addressService;


    /**
     * @Description 地址保存
     * @Author kang Tao
     * @Param [userId, address]
     * @return java.lang.Object
     * @Date 2022/4/22 21:40
     **/
    @PostMapping("/save")
    public Object saveAddress(@LoginUser Integer userId, @RequestBody Address address){

        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        Object validate = validate(address);

        if (validate != null){
            return validate;
        }

        if (address.getId() == null || address.getId().equals(0)) {
            if (address.getIsDefault()) {
                // 重置其他收货地址的默认选项
                addressService.resetDefaultAddress(userId);
            }

            address.setId(null);
            address.setUserId(userId);
            addressService.addAddress(address);
        } else {
            Address litemallAddress = addressService.queryAddress(userId, address.getId());
            if (litemallAddress == null) {
                return ResponseUtil.badArgumentValue();
            }

            if (address.getIsDefault()) {
                // 重置其他收货地址的默认选项
                addressService.resetDefaultAddress(userId);
            }

            address.setUserId(userId);
            addressService.updateAddress(address);
        }
        return ResponseUtil.ok(address.getId());

    }

    //对数据进行验证  电话号码 地址 不为空
    private Object validate(@RequestBody Address address) {
        String name = address.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }

        // 测试收货手机号码是否正确
        String mobile = address.getTel();
        if (StringUtils.isEmpty(mobile)) {
            return ResponseUtil.badArgument();
        }
        if (!RegexUtil.isMobileSimple(mobile)) {
            return ResponseUtil.badArgument();
        }

        String province = address.getProvince();
        if (StringUtils.isEmpty(province)) {
            return ResponseUtil.badArgument();
        }

        String city = address.getCity();
        if (StringUtils.isEmpty(city)) {
            return ResponseUtil.badArgument();
        }

        String county = address.getCounty();
        if (StringUtils.isEmpty(county)) {
            return ResponseUtil.badArgument();
        }


        String areaCode = address.getAreaCode();
        if (StringUtils.isEmpty(areaCode)) {
            return ResponseUtil.badArgument();
        }

        String detailedAddress = address.getAddressDetail();
        if (StringUtils.isEmpty(detailedAddress)) {
            return ResponseUtil.badArgument();
        }

        Boolean isDefault = address.getIsDefault();
        if (isDefault == null) {
            return ResponseUtil.badArgument();
        }
        return null;
    }


    @GetMapping("/list")
    public Object list(@LoginUser Integer userId){

        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        List<Address> addressList = addressService.queryByUid(userId);
        return ResponseUtil.okList(addressList);
    }


    /**
     * 收货地址详情
     *
     * @param userId 用户ID
     * @param id     收货地址ID
     * @return 收货地址详情
     */
    @GetMapping("detail")
    public Object detail(@LoginUser Integer userId, @NotNull Integer id) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        Address address = addressService.query(userId, id);
        if (address == null) {
            return ResponseUtil.badArgumentValue();
        }
        return ResponseUtil.ok(address);
    }

}
